package com.zwj.web;

import com.zwj.bean.Book;
import com.zwj.bean.Page;
import com.zwj.service.BookService;
import com.zwj.service.Impl.BookServiceImpl;
import com.zwj.utils.WebUtils;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * @author zwj
 * @create 2021-05-13-16:31
 */
public class BookServlet extends BaseServlet{
    private BookService bookService = new BookServiceImpl();

    /**
     * 添加图书,通过book_edit.jsp的form提交请求访问
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void add(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取请求参数
        int pageNo = WebUtils.parseInt(req.getParameter("pageNo"),0);
        pageNo+=1;
        //获取所有请求参数，封装到map
        Map<String, String[]> parameterMap = req.getParameterMap();
        //添加图书
        Book book = WebUtils.copyParamToBean(parameterMap, new Book());
        bookService.addBook(book);
        //请求重定向/manager/bookServlet?action=page
        resp.sendRedirect(req.getContextPath()+"/manager/bookServlet?action=page&pageNo="+pageNo);
    }

    /**
     * 删除图书,通过book_manager.jsp的删除请求访问
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取请求参数
        String id = req.getParameter("id");
        int parseInt = WebUtils.parseInt(id, 0);
        //删除图书
        bookService.deleteBookById(parseInt);
        //请求重定向/manager/bookServlet?action=page
        resp.sendRedirect(req.getContextPath()+"/manager/bookServlet?action=page&pageNo="+req.getParameter("pageNo"));
    }

    /**
     * 获取图书,通过book_manager.jsp的修改请求访问
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void getBook(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取请求参数
        String id = req.getParameter("Id");
        int parseInt = WebUtils.parseInt(id, 0);
        //查询图书
        Book book = bookService.queryBookById(parseInt);
        //保存到pagecontext域中
        req.setAttribute("book",book);
        //请求转发到/pages/manager/book_edit.jsp
        req.getRequestDispatcher("/pages/manager/book_edit.jsp").forward(req,resp);
    }

    /**
     * 更新图书,添加图书,通过book_edit.jsp的form提交请求访问
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 1、获取请求的参数==封装成为 Book 对象
        Book book = WebUtils.copyParamToBean(req.getParameterMap(),new Book());
         // 2、调用 BookService.updateBook( book );修改图书
        bookService.updateBook(book);
         // 3、重定向回图书列表管理页面
        // 地址：/工程名/manager/bookServlet?action=list
        resp.sendRedirect(req.getContextPath() + "/manager/bookServlet?action=page&pageNo="+req.getParameter("pageNo"));
    }

    /**
     * 显示所有图书
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void list(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        List<Book> books = bookService.queryBooks();
        //System.out.println(books.size());
        req.setAttribute("books",books);
        req.getRequestDispatcher("/pages/manager/book_manager.jsp").forward(req,resp);
    }

    /**
     * 显示页图书数据,通过manager_menu.jsp,
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void page(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取请求参数
        int pageNoInt = WebUtils.parseInt(req.getParameter("pageNo"), 1);
        int pageSizeInt = WebUtils.parseInt(req.getParameter("pageSize"), Page.PAGE_SIZE);
        //获取分页书籍对象
        Page<Book> page = bookService.page(pageNoInt, pageSizeInt);
        page.setUrl("manager/bookServlet?action=page");
        //存入域中
        req.setAttribute("page",page);
        //请求转发到/pages/manager/book_manager.jsp
        req.getRequestDispatcher("/pages/manager/book_manager.jsp").forward(req,resp);
    }
}
